package nl.rainbowsheep.reisorg.cust.site.controller.helper;

import com.tunyk.currencyconverter.BankUaCom;
import com.tunyk.currencyconverter.api.Currency;
import com.tunyk.currencyconverter.api.CurrencyConverter;
import com.tunyk.currencyconverter.api.CurrencyConverterException;
import com.tunyk.currencyconverter.api.CurrencyNotSupportedException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

public  class ConvertCurrency {

    private float amount;
    private float convertedAmount;
    private Currency toCurrency;
    private String currencySign = "€";

    public ConvertCurrency(float amount, String currencycookie) throws CurrencyNotSupportedException {
        String[] cookies = currencycookie.split("-");
        String currency = cookies[0];
        Currency newCurrency = Currency.fromString(currency);        
        this.toCurrency = newCurrency;
        this.amount = this.Convert(amount);
        this.currencySign ="&#" + cookies[1] + ";";
    }
    
    private float Convert(float amountToConvert){
        // Create an instance where EUR is a default currency to convert from, and the given Currency to is the one to convert to
        CurrencyConverter currencyConverter = null;
        try {
            currencyConverter = new BankUaCom(Currency.EUR, this.toCurrency);
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);
            float x = currencyConverter.convertCurrency(amountToConvert);
            this.convertedAmount = x;
        } catch (CurrencyConverterException ex) {
            Logger.getLogger(ConvertCurrency.class.getName()).log(Level.SEVERE, null, ex);
        }
        return convertedAmount;
    }

    public float getConvertedAmount() {
        return convertedAmount;
    }

    public void setConvertedAmount(float convertedAmount) {
        this.convertedAmount = convertedAmount;
    }

    public float getAmount() {
        return amount;
    }

    public void setAmount(float amount) {
        this.amount = amount;
    }

    public Currency getToCurrency() {
        return toCurrency;
    }

    public void setToCurrency(Currency toCurrency) {
        this.toCurrency = toCurrency;
    }

    public String getCurrencySign() {
        return currencySign;
    }

    public void setCurrencySign(String currencySign) {
        this.currencySign = currencySign;
    }
    
}

